﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using BCGC.BLL.Helper;
using BCGC.BLL.BusinessObject;
using Jalal.Library.Utility;

public partial class Admin_EmailTemplates_EditEmailTemplate : AdminBasePage
{
    private int Id
    {
        get 
        {
            if (ViewState["Id"] == null)
                ViewState["Id"] = 0;
            return (int)ViewState["Id"]; 
        }
        set { ViewState["Id"] = value; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            int id = Id = 0;
            if (!string.IsNullOrEmpty(Request.QueryString["id"]) && int.TryParse(Request.QueryString["id"], out id))
            {
                Id = id;
                LoadData();
            }
            else
                Response.Redirect(CustomSiteMap.EmailTemplates);
        }
    }

    private void LoadData()
    {
        EmailTemplate emailTemplate = EmailTemplate.GetEmailTemplateById(Id);
        if (emailTemplate == null)
            Response.Redirect(CustomSiteMap.EmailTemplates);
        else
        {
            ddlTemplateTitle.SelectedValue = emailTemplate.MailTitle;
            tbxTemplateSubject.Text = emailTemplate.MailSubject;
            fckMailTemplateBody.Value = Server.HtmlDecode(emailTemplate.MailBody);
        }
    }

    protected void Update_Click(object sender, EventArgs e)
    {
        if (IsValid)
        {
            if (EmailTemplate.UpdateEmailTemplate(Id, Security.BindStringInput(ddlTemplateTitle.SelectedValue,
                SecurityLevel.Three), Security.BindStringInput(tbxTemplateSubject.Text, SecurityLevel.Three),
                Security.BindStringInput(fckMailTemplateBody.Value, SecurityLevel.Zero)))
            {
                uccResponse.SetSuccessMessage("Email template successfuly updated");
            }
            else
                uccResponse.SetFailMessage("Failed to update email template");
        }
    }
}
